<?php

namespace app\api\controller;

use app\common\lib\ArrClass;
use app\common\lib\Show;
use app\common\lib\Status;
use think\Exception;

class Upload extends Base
{

    public function image(){
        if (!$this->request->isPost()) {
            throw new Exception("请求类型错误", 400);
        }
        $file = $this->request->file();
        if (empty($file)) {
            return Show::json_show(Status::mini_error, "请上传图片");
        }
        $key = array_keys($file);
        $arrType = ArrClass::uploadAttr();
        try {
            $objClass = ArrClass::initClass($key[0], $arrType, [], true);
            $res = $objClass->upload($this->request);
        }catch (\Exception $e){

            return Show::json_show(Status::mini_error,$e->getMessage(),[],400);
        }
        if($res){
            return Show::json_show(Status::mini_normal,"上传成功",[
                "url"=>(string)url('/'.$res,[],false,true)
            ]);// tp6自带全路径
        }else{
            return Show::json_show(Status::mini_error,'上传失败！请重试');
        }
    }
}